package service.impl;

import nl.justobjects.pushlet.pushmanager.DataCreator;
import nl.justobjects.pushlet.pushmanager.PushManager;
import nl.justobjects.pushlet.wrapper.JMSService;
import service.Chat;
import web.User;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ChatImpl implements Chat {

	private JMSService jmsService;
	
	public void setJmsService(JMSService jmsService) {
		this.jmsService = jmsService;
	}

	@Override
	public void sendMessage(Map<String, Object> msg) {
		String subject = (String) msg.get("subject");
		String label = (String) msg.get("label");
		String pid = (String) msg.get("pid");
		@SuppressWarnings("unchecked")
		Map<String, Serializable> data = (Map<String, Serializable>) msg.get("data");
		
		// 插入到数据库 
		// ....
		
		// 调用jms接口，通知集群节点推送消息
		this.jmsService.broadcastEvent(subject, label, data);
	}

    public void createSourceThread(String threadId) {
        PushManager.getInstance().createEvent(threadId ,"pushthread", null, new DataCreator() {
            @Override
            public Map<String, Serializable> createPushData(Map<String, Object> params) {
                Map<String, Serializable> re = new HashMap<String, Serializable>();
                re.put("now", "现在时间：" + new Date().toString());
                ArrayList<User> users = new ArrayList<User>();
                users.add(new User("xnkl1", 11));
                users.add(new User("xnkl2", 122));
                re.put("al", users);
                return re;
            }
        }, null, null, 10000, 360000);
    }

}
